#include "TinyXMLDan.h"
#include "../TinyXML/tinyxml.h"
#include "../TinyXML/tinystr.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "Animation.h"
#include <string>
#include <vector>
#include "Frame.h"
#include <cassert>


TinyXMLDan::TinyXMLDan(void)
{
}


TinyXMLDan::~TinyXMLDan(void)
{
}

Animation TinyXMLDan::Load(char * FilePath)
{

	//Temp Storage Variables
	int NumFrames;
	D3DXCOLOR IgnoredColor;
	int IgnoredRed;
	int IgnoredGreen;
	int IgnoredBlue;

	std::string ImagePath;
	//std::vector<Frame> FrameVec;

	//Start Tiny XML
	// Create the TinyXML Document
	TiXmlDocument doc;

	// Attempt to load
	if( doc.LoadFile( FilePath ) == false )
		assert("Anim - Loading - Failed to load the file.");


	// Access the root node
	TiXmlElement* pRoot = doc.RootElement();

	//Make sure the Root is valid
	if( pRoot == nullptr )
		assert("Anim - Loading - Root is NULL! ");

	// Loop through the root's children
	TiXmlElement* pRootChildren = pRoot->FirstChildElement();
	TiXmlElement* pAnimChildren;
	Animation TempAnimation;

	//Drop iterator to the current animation
	pAnimChildren = pRootChildren->FirstChildElement();

	//Drop the iterator to the current Anum Childrin
	//pAnimChildren = pAnimChildren->FirstChildElement();

	//Attribute name
	string FramCountAttName = "num";

	NumFrames = 0;
	//Store the NumFrames
	pAnimChildren->QueryIntAttribute(FramCountAttName.c_str(), &NumFrames);

	//Move along
	pAnimChildren = pAnimChildren->NextSiblingElement();

	//Load ignored color
	pAnimChildren->QueryIntAttribute("r", &IgnoredRed);
	pAnimChildren->QueryIntAttribute("g", &IgnoredGreen);
	pAnimChildren->QueryIntAttribute("b", &IgnoredBlue);

	//Create the D3DColor from the ignored colors
	IgnoredColor = D3DCOLOR_ARGB(255,IgnoredRed, IgnoredGreen, IgnoredBlue);

	//Move Along
	pAnimChildren = pAnimChildren->NextSiblingElement();

	//Read image path
	ImagePath = pAnimChildren->GetText();

	//Move Along
	pAnimChildren = pAnimChildren->NextSiblingElement();

	//Move down to frames 
	TiXmlElement* pCurrFrame = pAnimChildren->FirstChildElement();

	//Load Frames
	for ( int i = 0; i < NumFrames; i++ )
	{
		if( pCurrFrame == nullptr)
			continue;

		//Set up a Temp Frame
		Frame TempFrame;

		//Set Defaults
		TempFrame.SetHitBox(1,1,1,1);
		TempFrame.SetAnchorPoint(1,1);
		TempFrame.SetDuration(1);


		//Set up  a Frame Child Element iter
		TiXmlElement* pFramesChildren;
		pFramesChildren = pCurrFrame->FirstChildElement();

		//Temp Data for Anchor Point
		int AnchorX = 0;
		int AnchorY = 0;

		//Load in Anchor Point
		pFramesChildren->QueryIntAttribute("x", &AnchorX);
		pFramesChildren->QueryIntAttribute("y", &AnchorY);

		//Set Anchor
		TempFrame.SetAnchorPoint(AnchorX, AnchorY);

		//Move Along
		pFramesChildren = pFramesChildren->NextSiblingElement();

		//Declare and init col box vals
		int left = 0;
		int top = 0;
		int right = 0;
		int bottom = 0;

		//Load in HitBox elements
		pFramesChildren->QueryIntAttribute("left", &left);
		pFramesChildren->QueryIntAttribute("top", &top);
		pFramesChildren->QueryIntAttribute("right", &right);
		pFramesChildren->QueryIntAttribute("bottom", &bottom);

		//Store hit box
		TempFrame.SetHitBox(left,top,right,bottom);

		//Move forward to the source rect
		pFramesChildren = pFramesChildren->NextSiblingElement();

		//Declare and init col box vals
		int Sourceleft = 0;
		int Sourcetop = 0;
		int Sourceright = 0;
		int Sourcebottom = 0;

		//Load in Source Rect elements
		pFramesChildren->QueryIntAttribute("left", &Sourceleft);
		pFramesChildren->QueryIntAttribute("top", &Sourcetop);
		pFramesChildren->QueryIntAttribute("right", &Sourceright);
		pFramesChildren->QueryIntAttribute("bottom", &Sourcebottom);

		//Store Source Rect
		TempFrame.SetSourceRect(Sourceleft,Sourcetop,Sourceright,Sourcebottom);

		//Move Forward
		pFramesChildren = pFramesChildren->NextSiblingElement();

		//Load float
		float TempDuration = 0.0f;
		pFramesChildren->QueryFloatAttribute("dur",&TempDuration);

		//Store Duration
		TempFrame.SetDuration(TempDuration);

		//Shoulder point
		if( pFramesChildren->NextSiblingElement() != nullptr)
			pFramesChildren = pFramesChildren->NextSiblingElement();

		if( pFramesChildren->NextSiblingElement() != nullptr)
			pFramesChildren = pFramesChildren->NextSiblingElement();

		if( pFramesChildren->NextSiblingElement() != nullptr)
			pFramesChildren = pFramesChildren->NextSiblingElement();

		int ShoulderX = 0;
		int ShoulderY = 0;

		pFramesChildren->QueryIntAttribute("ShoulderX", &ShoulderX);
		pFramesChildren->QueryIntAttribute("ShoulderY", &ShoulderY);

		TempFrame.SetShoulderPoint(ShoulderX, ShoulderY);


		//Move Forward
		//pFramesChildren = pFramesChildren->NextSiblingElement();

		//Event Name
		//std::string FrameEvent = pFramesChildren->GetText();

		//Move Forward
		//pFramesChildren = pFramesChildren->NextSiblingElement();

		//Event Point
		//int LoadedEventPointX = 0;
		//int LoadedEventPointY = 0;

		//pFramesChildren->QueryIntAttribute("EventX", &LoadedEventPointX);
		//pFramesChildren->QueryIntAttribute("EventY", &LoadedEventPointY);


		//Store new frame
		//FrameVec.push_back(TempFrame);
		TempAnimation.AddFrame(TempFrame);

		//Next Frame
		pCurrFrame = pCurrFrame->NextSiblingElement();

	}
	TempAnimation.SetIgnoredColor( IgnoredColor );
	TempAnimation.SetFrameCount( NumFrames );
	//TempAnimation.SetFrames( FrameVec );
	std::string LoadedFilePath = "Textures\\" + ImagePath;
	TempAnimation.SetImagePath( LoadedFilePath );


	//Update the Animation we are on
	//pRootChildren = pRootChildren->NextSiblingElement();

	//Break out of loop
	//if(pRootChildren == nullptr)
	//	break;

	return TempAnimation;

}



